# -*- coding: utf-8 -*- 
# @Time : 2021/11/5 17:51 
# @Author : Orange
# @File : get_common_data.py

from P_zong.constant_value import *
import requests

def get_basic_points(stationId, deviceId):
    """
    :param stationId:
    :param deviceId: 为ELEC树一级进线的deviceId
    :return: 获取测点名称和属性接口
    """
    d = {
        "deviceId": deviceId,
        "stationId": stationId
    }
    headers = {"ticket": "123456"}

    url = OPERATION_DOMAIN + '/ops/curvemonitor/queryMetricItemForCurveMonitor'

    r = requests.post(url, json=d, headers=headers)
    return r.json()


def P_is_basic_point(stationId, deviceId):
    """
    :param stationId:
    :param deviceId: 为ELEC树一级进线的deviceId
    :return: flag_get_result_form_bigdata:是否从大数据取数据标志位（为1，从大数据取；为0，从指标数据取）, equipID：设备ID, equipMK：设备类型
    """
    data = get_basic_points(stationId=stationId, deviceId=deviceId)
    data_basic = [i["metricDataDtos"] for i in data["data"] if i["itemName"] == "基础测点"][0]  # 获取基础测点名称
    data_P_all = [i['metricDataDtos'] for i in data_basic if i["groupName"] == "有功功率"][0]  # 获取所有的功率基础测点名
    data_P = [i["pointCodeForBigdata"] for i in data_P_all if i["code"] == "P" and i["storagePosition"] == "opents_db"]  # 若为空，说明为指标测点
    flag_get_result_form_bigdata = 0  # 是否从大数据获取数据标志位
    equipMK = ""
    equipID = ""
    if data_P:  # 非空，为基础测点
        flag_get_result_form_bigdata = 1
        if "METE" in data_P[0]:
            equipMK = data_P[0][:4]
            equipID = data_P[0][5:-2]
        else:
            equipMK = "ELEC"
            equipID = deviceId
    return flag_get_result_form_bigdata, equipID, equipMK



def get_cim_data(systemCode):
    '''
    :param systemCode:
    :return: 获取CEC树的CIM数据
    '''
    d = {
        "inPointCodes": [],
        "stationId": systemCode,
        "treeType": "ELEC"
    }

    url = CIM_DOMAIN + '/cim/build/deviceTree/by/station/deviceType/temp'

    r = requests.post(url, json=d)
    return r.json()

def get_cim_data_cec(systemCode):
    d = {
        "inPointCodes": [],
        "stationId": systemCode,
        "treeType": "CEC"
    }

    url = CIM_DOMAIN + '/cim/build/deviceTree/by/station/deviceType/temp'

    r = requests.post(url, json=d)
    return r.json()

# 获取所有的一级表的deviceId
def getDeviceId(cim_data):
    """

    :param systemCode:
    :param cim_data:
    :return: 获取所有总进线设备名称列表
    """

    try:
        # 一级表计
        data = cim_data["data"]["deviceTreeBoList"]
        # 所有一级表计的deciceId组成的列表
        deviceId = [data[i]["deviceTreeTplgyBo"]["deviceId"] for i in range(len(data))]
        if not deviceId:
            """若一级进线不存在，说明没有建树"""
            raise ValueError("缺乏物联数据")
    except:
        raise ValueError("缺乏物联数据")
    return deviceId


def get_device_diff_list(stationId,cim_data):
    """
    :param stationId:
    :return: device_id_list:从指标数据取功率的设备名称,equipID_list：从大数据取数据的设备名称,equipMK_list：从大数据取数据设备类型
    """
    # 判断device_id列表里的总进线功率是否属于基础测点
    device_id= getDeviceId(cim_data)
    equipID_list = []
    equipMK_list = []
    device_id_list = []
    for i in device_id:
        flag_get_result_form_bigdata, equipID, equipMK = P_is_basic_point(stationId=stationId, deviceId=i)
        if flag_get_result_form_bigdata:
            equipID_list.append(equipID)
            equipMK_list.append(equipMK)
        else:
            device_id_list.append(i)
    return device_id_list, equipID_list, equipMK_list,device_id

